/* USER CODE BEGIN 0 */
#include "main.h"
#include "Timer.h"
#include "uart.h"
#include "ADScan.h"
#include "fan_ctrl.h"

/* USER CODE END 0 */
 TIM_HandleTypeDef htim2;
 TIM_HandleTypeDef htim5;
	
	
/* USER CODE BEGIN 1 */
volatile 	unsigned int	counter,counterTIM4;
volatile 	unsigned short int	counter_16;

/**
  * @brief  Period elapsed callback in non blocking mode
  * @note   This function is called  when TIM6 interrupt took place, inside
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  * a global variable "uwTick" used as application time base.
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */
	if (htim == (&htim2))
	{
		counter_16++;
		counter++;
		Uart_IdleSpan_Increase();
		DcFan1_Ctrl();
		DcFan2_Ctrl();
	}
	
	/* USER CODE END Callback 0 */
	if (htim->Instance == TIM6) 
	{
		HAL_IncTick();
		Fan_FeedBack_Calc();		
	}
}


/*
 * Function Name:	Timer2_Get16Ticks
 * Description:	��ȡ16λ������
 * Return Value:	unsigned int	����16λ�޷�����
 * Parameter:	��
 * Note:
 */
u16 Timer2_Get16Ticks(void)
{
	u16	Res;
	//__disable_irq(); 
	Res = counter_16;
	//__enable_irq(); 
	return Res;
}


/*
 * Function Name:	Timer2_Get16Ticks
 * Description:	��ȡ16λ������
 * Return Value:	unsigned int	����16λ�޷�����
 * Parameter:	��
 * Note:
 */
unsigned int Timer2_Get32Ticks(void)
{
	u32	Res;
	//__disable_irq(); 
	Res = counter;
	//__enable_irq(); 
	return Res;
}


void HAL_Delay_us(uint16_t us)
{
    uint16_t delay = 0xffff-us-5;		//�Ӵ������м������������65535����պ�1us	
    										
    HAL_TIM_Base_Start(&htim5);  	    //����������
    __HAL_TIM_SET_COUNTER(&htim5,delay);	//���ü�����
	
    while(delay<0xffff-5 && delay>=0xffff-us-5)
    {
        delay = __HAL_TIM_GET_COUNTER(&htim5); //��ȡ��ǰ����ֵ
    }
    HAL_TIM_Base_Stop(&htim5);
}


void delay_us(uint32_t nus)
{		
	HAL_Delay_us(nus);	    
}




void delay_ms(uint16_t nms)
{
	uint32_t i;
	//for(i=0;i<nms;i++) delay_us(1000);
	HAL_Delay(nms);
}



/**
  * @brief TIM5 Initialization Function
  * @param None
  * @retval None
  */
void MX_TIM5_Init(void)
{

  /* USER CODE BEGIN TIM5_Init 0 */

  /* USER CODE END TIM5_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM5_Init 1 */

  /* USER CODE END TIM5_Init 1 */
  htim5.Instance = TIM5;
  htim5.Init.Prescaler = 71;
  htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim5.Init.Period = 1;
  htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim5) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim5, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM5_Init 2 */

  /* USER CODE END TIM5_Init 2 */

}




/* USER CODE END 1 */

/**
  * @brief TIM2 Initialization Function
  * @param None
  * @retval None
  */
void MX_TIM2_Init(void)
{

  /* USER CODE BEGIN TIM2_Init 0 */

  /* USER CODE END TIM2_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM2_Init 1 */

  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 71;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 999;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
  HAL_TIM_Base_Start_IT(&htim2);
  /* USER CODE END TIM2_Init 2 */

}


